var timeTrue = true;
var meter;
var report = {
    initPage: function () {
        report.lastBuild();
        report.getBuildTestRightMenu();
    },
    /**
     * 不断的请求后端服务，获取最新的执行结果
     * **/
    buildTestRefresh: function () {
        //js的定时任务，第一个参数代表要执行的方法,第二个参数代表时间,单位:毫秒
        //不是完成的定时任务，只是代表延迟执行。
        report.getReportLog();
        report.getBuildTestRightMenu();
        report.getBuildTest();
        if (timeTrue) {
            meter = setTimeout("report.buildTestRefresh()", 2000)
        }
    }
    /**实时的获取服务端的执行结果,并且刷新到页面上**/
    , getBuildTest: function () {
        var url = "/report/getBuildTest";
        var buildTestId = $("#buildTestId").val();
        $.ajax({
            url: url,
            type: "get",
            dataType: "json",
            data: {
                buildTestId: buildTestId
            },
            success: function (result) {
                if (result.success) {
                    var buildTest = result.data;
                    try {
                        var runStatus = buildTest.runStatus;
                        if (runStatus == 8) {
                            timeTrue = false;
                            clearTimeout(meter);
                        }
                    } catch (e) {
                        timeTrue = false;
                        clearTimeout(meter);
                    }
                    report.writeBuildTest(buildTest);
                } else {
                    timeTrue = false;
                    clearTimeout(meter);
                    Ewin.alert(result.msg);
                }
            }, error: function () {
                timeTrue = false;
                clearTimeout(meter);
            }
        })
    }
    ,
    /**
     * 获取最后一次运行的数据
     * **/
    lastBuild: function () {
        var url = "/report/lastBuild";
        $.ajax({
            url: url,
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.success) {
                    var buildTest = result.data;
                    $("#buildTestId").val(buildTest.id);
                    report.getReportLog();
                    report.writeBuildTest(buildTest);
                }
            }
        })
    },
    detailPage: function (buildTestInterfaceId) {
        window.location.href = "/report/reportDetailPage?buildTestInterfaceId=" + buildTestInterfaceId
    }
    ,
    writeBuildTest: function (buildTest) {
        $("#testPlanName").text(buildTest.testPlanName);
        $("#startTime").text(buildTest.startTime);
        $("#runTime").text(buildTest.runTime);
        $("#userName").text(buildTest.userName);
        $("#testPlanName").text(buildTest.testPlanName);
        $("#caseSize").text(buildTest.caseSize);
        var runStatus =buildTest.runStatus;
        $("#status").empty();
        if(runStatus ==8){
            var status = buildTest.status;
            $("#status").append(report.statusSwitch(status));
        }else {
            $("#status").append(report.statusSwitch(runStatus));
        }
        report.writePieChart(buildTest);
        var buildTestInterfaceList = buildTest.buildTestInterfaceDoList;
        $("#interfaceList").empty();
        $("#interfaceSize").text(buildTestInterfaceList.length);

        buildTestInterfaceList.forEach(function (interface) {
            var passCaseSize = interface.passCaseSize;
            if (null == passCaseSize) {
                passCaseSize = 0;
            }
            var failCaseSize = interface.failCaseSize;
            if (null == failCaseSize) {
                failCaseSize = 0;
            }
            var buildTestInterfaceId = interface.id;
            var buildTestInterfaceAliasName = interface.interfaceAliasName;
            var tag = "<div class=\"row\" style=\"margin-bottom: 5px\">" +
                "                        <div class=\"col-md-5\">\n" +
                "                            <a href=\"#\" onclick='report.detailPage(" + buildTestInterfaceId + ")'>" + buildTestInterfaceAliasName + "</a>\n" +
                "                        </div>\n" +
                "                        <div class=\"col-md-7\">\n" +
                "                            <div class=\"bar\">\n" +
                "                                <div class=\" bar__fill_status_failed\" style=\"flex-grow: 1;text-align: center\">\n" +
                "                                    <span style=\"color: #fafafa\">" + failCaseSize + "</span></div>\n" +
                "                                <div class=\" bar__fill_status_passed\" style=\"flex-grow: 1;text-align: center\">\n" +
                "                                    <span style=\"color: #fafafa\">" + passCaseSize + "</span></div>\n" +
                "                            </div>\n" +
                "                        </div>\n" +
                "                    </div>";
            $("#interfaceList").append(tag);
        })
    },
    writePieChart: function (buildTest) {
        var passRate = buildTest.passRate;
        if (null == passRate) {
            passRate = 100;
        }
        passRate = passRate.toFixed(2);
        var data = [{label: "失败率", value: 100 - passRate},
            {label: "通过率", value: passRate}];   //练习的json数据
        var donut = new Morris.Donut({
            element: 'pass-rate',  //绑定的元素
            resize: true,           //是否可以重置
            colors: ["#f56954", "#00a65a"], //颜色
            data: data,                   //数据
            formatter: function (y) {  //格式化增加百分号
                return y + "%";
            }
        });
    }
    ,
    /**
     * 获取状态标签
     * **/
    statusSwitch: function (status) {
        var tag;
        switch (status) {
            case -1:
                tag = "<img src='/images/status/red.gif'>执行异常";
                break;
            case 1:
                tag = "<img src='/images/status/green.gif'>全部通过";
                break;
            case 2:
                tag = "<img src='/images/status/red.gif'>全部失败";
                break;
            case 3:
                tag = "<img src='/images/status/yellow.gif'>部分失败";
                break;
            default:
                tag = "<img src='/images/status/blue_anime.gif'>运行中";
                break;
        }
        return tag;
    },
    /**
     * 获取执行日志
     * **/
    getReportLog: function () {
        var url = "/runTest/runLog";
        var buildTestId = $("#buildTestId").val();
        var lastTimeFileSize = $("#lastTimeFileSize").val();
        $.ajax({
            url: url,
            type: "get",
            data: {
                buildTestId: buildTestId,
                lastTimeFileSize: lastTimeFileSize
            }, success: function (result) {
                if (null != result) {
                    $("#reportLog").append(result.reportText);
                    $("#lastTimeFileSize").val(result.lastTimeFileSize);
                }
            }
        })
    },
    /**
     *最右侧的执行历史列表
     * **/
    getBuildTestRightMenu: function () {
        var url = "/report/getBuildTestRightMenu";
        $.ajax({
            url: url,
            type: "get",
            data: {}, success: function (result) {
                if (result.success) {
                    $("#history").empty();
                    var data = result.data;
                    data.forEach(function (buildTest) {
                        var start = "<div>";
                        var status = "";
                        var runStatus =buildTest.runStatus;
                        if(null ==runStatus ||runStatus ==8){
                            switch (buildTest.status) {
                                case 1:
                                    //全部通过
                                    status += "<img src ='/images/status/green.gif'>"
                                    break;
                                case 2:
                                    status += "<img src ='/images/status/red.gif'>"
                                    //全部失败
                                    break;
                                case 3:
                                    status += "<img src ='/images/status/yellow.gif'>"
                                    //部分失败
                                    break;
                                case 4:
                                    status += "<img src ='/images/status/blue_anime.gif'>"
                                    break;
                                //执行中
                                default:
                                    status += "<img src ='/images/status/blue_anime.gif'>"
                                //执行异常
                            }
                        }else {
                            status += "<img src ='/images/status/blue_anime.gif'>"
                        }

                        //TODO 等待页面出来在增加点击事件
                        var startTime = buildTest.startTime;
                        if (null == startTime) {
                            startTime = getFormatDate();
                        }
                        var a = "<a onclick='report.goHistory(" + buildTest.id + ")'>" + startTime + "</a>"
                        var end = "</div>"
                        $("#history").append(start + status + a + end);
                    })
                } else {
                    Ewin.alert(result.msg);
                }
            }
        })
    },
    goHistory: function (buildTestId) {
        window.location.href = "/report/reportPage?buildTestId=" + buildTestId;
    }
};